
clear;
idle_prob=rand(1,12);%[0.2 0.6 0.9];%[0.5 0.9];%
SNR_var=10^1;
frame_duration=100;
sensing_duration=1;
N_channel=length(idle_prob);
rate_expect=zeros(1,N_channel+1);
for iteration=N_channel:-1:1
    rate_expect(iteration)=Expected_Rate(idle_prob(iteration),SNR_var,frame_duration,sensing_duration,iteration,rate_expect(iteration+1));
end
disp([num2str(idle_prob) ':' num2str(rate_expect)]);

%% Searching for the optimal sensing sequence by Dynamic Programming
if 1
    tic
[ Optimal_Sensing_Seq Optimal_Rate_Expect] = DSA_Seq_DynamicProgramming( idle_prob,SNR_var,frame_duration,sensing_duration );
idle_prob(Optimal_Sensing_Seq)
Optimal_Rate_Expect
toc
end
%% Searching for the optimal sensing sequence by Brute Force
if 0
idle_prob_Perms = perms(idle_prob);
rate_expect_Perms=zeros(size(idle_prob_Perms,1),1);
Perms_num=size(idle_prob_Perms,1);
tic
parfor iiPerms = 1:Perms_num
    if ~mod(iiPerms,100)
        iiPerms/Perms_num
    end
    idle_prob_tmp=idle_prob_Perms(iiPerms,:);
    rate_expect_tmp=zeros(1,N_channel+1);
    for iteration=N_channel:-1:1
        rate_expect_tmp(iteration)=Expected_Rate(idle_prob_tmp(iteration),SNR_var,frame_duration,sensing_duration,iteration,rate_expect_tmp(iteration+1));
    end
    rate_expect_Perms(iiPerms)=rate_expect_tmp(1);
end
[rate_expect_max rate_expect_max_idx]=max(rate_expect_Perms);
idle_prob_Perms(rate_expect_max_idx,:)
toc
end